package xyz.jcat.biz.admin.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import xyz.jcat.web.security.AccountAuthenticationProvider;
import xyz.jcat.web.security.DefaultAccessDeniedHandler;
import xyz.jcat.web.security.DefaultAuthenticationEntryPoint;
import xyz.jcat.web.security.DefaultAuthenticationFailureHandler;
import xyz.jcat.web.security.DefaultCorsConfigurationSource;
import xyz.jcat.web.security.DefaultLogoutSuccessHandler;
import xyz.jcat.web.security.HttpHeaderAccessTokenFilter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    //TODO 配置
    private static final String[] permitPaths = {"/rsa-pks"};
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //不使用httpsession管理token
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                //请求白名单
                .antMatchers(permitPaths).permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                //登录成功处理类
                .successHandler(new AuthenticationSuccessHandlerImpl())
                //登录失败处理类
                .failureHandler(new DefaultAuthenticationFailureHandler())
                .and()
                //退出登录处理类
                .logout().logoutSuccessHandler(new DefaultLogoutSuccessHandler())
                .and()
                .exceptionHandling()
                //未认证异常处理类
                .authenticationEntryPoint(new DefaultAuthenticationEntryPoint())
                //未授权异常处理类
                .accessDeniedHandler(new DefaultAccessDeniedHandler())
                .and()
                .csrf().disable()
                .httpBasic().disable()
                .cors().disable()
        ;
        //添加token过滤器
        http.addFilterBefore(new HttpHeaderAccessTokenFilter(), UsernamePasswordAuthenticationFilter.class);
        //开启跨域支持
        http.cors().configurationSource(new DefaultCorsConfigurationSource().corsConfigurationSource());

        http.headers().cacheControl();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new AccountAuthenticationProvider(userDetailsService));
    }

}
